﻿using System;
using System.IO;

namespace WikiDoc
{
    /// <summary>
    /// 用于从 XML Doc 中提取指定元素的内容。
    /// </summary>
    public class XmlDocPicker
    {
        /// <summary>
        /// 初始化 <see cref="XmlDocPicker"/> 类的新实例。
        /// </summary>
        /// <param name="elementName">Name of the element.</param>
        /// <param name="baseReader">The base reader.</param>
        public XmlDocPicker(string elementName, TextReader baseReader)
        {
            _ElementName = elementName;
            _StartSign = string.Format("<{0}>", elementName);
            _EndSign = string.Format("</{0}>", elementName);

            _SummaryPicker = new Reader.SummaryPicker(baseReader, string.Format("</{0}>", elementName));
            _TrimReader = new Reader.TrimReader(_SummaryPicker, 0);
            _SummaryPicker.Append(_TrimReader);
        }

        private readonly string _ElementName;

        /// <summary>
        /// Gets the name of the element.
        /// </summary>
        /// <value>The name of the element.</value>
        public string ElementName
        {
            get { return _ElementName; }
        }

        private readonly string _StartSign;

        /// <summary>
        /// 元素开始标记，即 &lt;xxxx&gt;
        /// </summary>
        /// <value>The start sign.</value>
        public string StartSign
        {
            get { return _StartSign; }
        }

        private readonly string _EndSign;

        /// <summary>
        /// 元素的结束标记，即 &lt;/xxxx&gt;
        /// </summary>
        /// <value>The end sign.</value>
        public string EndSign
        {
            get { return _EndSign; }
        }

        /// <summary>
        /// 元素的缩进级别（空格数）
        /// </summary>
        /// <value>The indent level.</value>
        public int IndentLevel
        {
            get { return _TrimReader.TrimCount; }
            set { _TrimReader.TrimCount = value; }
        }

        /// <summary>
        /// 重置阅读器的状态，使之继续进行处理。
        /// </summary>
        public void Reset()
        {
            _SummaryPicker.Reset();
        }

        private readonly Reader.SummaryPicker _SummaryPicker;

        private readonly Reader.TrimReader _TrimReader;

        /// <summary>
        /// Gets the reader.
        /// </summary>
        /// <value>The reader.</value>
        public TextReader Reader
        {
            get { return _SummaryPicker; }
        }
    }
}
